package FilterDesigners.Butterworth;

public class HighpassButterworthFilter extends ButterworthFilter{

	public HighpassButterworthFilter(double w1, double w2, double k1, double k2) {
		super(w1, w2, k1, k2);
	}
	
	/*
	 * Calculating the 3-dB Frequency for a highpass filter is similar to calculating it for a lowpass filter.
	 * First, W1 and W2 need to be scaled by W2. Then the resulting Wc needs to be scaled back by W2.
	 */
	protected double calculate3dBFreq() {
		double W1p = W2/W2;
		double W2p = W2/W1;
		double wcPrimeExpected = W1p/Math.pow(Math.pow(10.0, K1/10.0) - 1, 1.0/(2.0*ORDER));
		double wc2PrimeExpected = W2p/Math.pow(Math.pow(10.0, K2/10.0) - 1, 1.0/(2.0*ORDER));
		double expectedFreqPrime = Math.sqrt(wcPrimeExpected*wc2PrimeExpected);
		return W2/expectedFreqPrime;
	}

	protected void calculateNormalizedComponents() {
		for(int i = 0; i < (ORDER - 1)*2; i++){
			if(i%2 == 0){	//Resistor values will be stored in even numbered indecies
				if((i+1)%4 == 1)	//If this is the 1st resistor in a four component set, it should be 1/(2*Q).
					normalizedComponents.add(1/(2.0*qValues.get(i/4)));
				else				//If this is the 2nd resistor in a four component set, it should be 2*Q.
					normalizedComponents.add(2.0*qValues.get(i/4));
			}
			else {	//Capacitor values will be stored in odd numbered indecies.
				normalizedComponents.add(1.0);
			}
		}
		if(ORDER % 2 == 1) {	//If the filter order is odd, tack on a passive section (R = 1, C = 1)
			normalizedComponents.add(1.0);
			normalizedComponents.add(1.0);
		}		
	}

}
